﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Thumbnailv2
{
    public class Thumbnail
    {
        /* HOW TO USE
                            Thumbnailv2.Thumbnail obj = new Thumbnailv2.Thumbnail();
                            obj.InputImage = "D:\\Templates\\doc.jpg";
                            obj.W = 540;
                            obj.H = 231;
                            obj.cropW = 210;
                            obj.cropH = 90;
                            obj.X = 0;
                            obj.Y = 290;
                            string filename = obj.CreateThumbByCoordinates();

                            obj.InputImage = "D:\\Templates\\ngang.jpg";
                            obj.W = 150;
                            obj.H = 150;
                            obj.CreateThumb();

                            obj.InputImage = "D:\\Templates\\vuong.jpg";
                            obj.W = 150;
                            obj.H = 150;
                            obj.CreateThumb();
        */

        #region public properties
        // for thumbnail
        public string InputImage { get; set; }
        public Int32 W { get; set; }
        public Int32 H { get; set; }

        // for crop by X, Y
        public Int32 X { get; set; }
        public Int32 Y { get; set; }
        public Int32 cropW { get; set; }
        public Int32 cropH { get; set; }
        #endregion

        public string CreateThumbByCoordinates()
        {
            if (!((X >= 0) & (Y >= 0)))
            {
                return "";
            }
            string filename = InputImage + ".cr" + cropW + "x" + cropH + ".jpg";
            FileInfo info = new FileInfo(InputImage);
            if (info.Exists)
            {
                try
                {
                    ImageCodecInfo info3 = ImageCodecInfo.GetImageEncoders()[1];
                    Image image = Image.FromFile(InputImage);
                    int width = image.Width;
                    int height = image.Height;

                    Bitmap bitmap = new Bitmap(cropW, cropH);
                    Graphics graphics = Graphics.FromImage(bitmap);
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    Rectangle destRect = new Rectangle(0, 0, cropW, cropH);
                    graphics.DrawImage(image, destRect, X, Y, W, H, GraphicsUnit.Pixel);
                    EncoderParameters encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                    bitmap.Save(filename, info3, encoderParams);
                    FileInfo info2 = new FileInfo(filename);
                    image.Dispose();
                    graphics.Dispose();
                    bitmap.Dispose();
                    return info2.Name;
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    throw exception;
                }
            }
            throw new Exception("Input file not found");

        }




        private Int32 nW = 0;
        private Int32 nH = 0;
        private Int32 nX = 0;
        private Int32 nY = 0;
        public string CreateThumb()
        {
            System.Drawing.Image oImg = System.Drawing.Image.FromFile(InputImage);
            CalculateDimension(oImg.Width, oImg.Height);
            System.Drawing.Image oThumbNail = new Bitmap(W, H, oImg.PixelFormat);

            string filename = InputImage + ".thumb" + W + "x" + H + ".jpg";


            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

            //Set the parameters for defining the quality of the thumbnail... here it is set to 100%
            ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[1];
            EncoderParameters eParams = new EncoderParameters(1);
            eParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

            Rectangle oRectangle = new Rectangle(nX, nY, nW, nH);
            oGraphic.DrawImage(oImg, oRectangle);
            oThumbNail.Save(filename, codec, eParams);
            oImg.Dispose();
            FileInfo info2 = new FileInfo(filename);
            return info2.Name;
        }

        private void CalculateDimension(Int32 wO, Int32 hO)
        {
            if((wO == W)&&(hO == H))
            {
                nW = W;
                nH = H;
                nX = 0;
                nY = 0;
                return;
            }

            if (wO > hO) // anh ngang
            {
                nH = H;
                nW = Convert.ToInt32(Math.Ceiling(((float)wO * nH) / hO));
                if (nW < W)
                {
                    nW = W;
                    nH = Convert.ToInt32(Math.Ceiling(((float)hO * W) / wO));
                    nX = 0;
                    nY = Convert.ToInt32(Math.Ceiling((float)(nH - H) / 2) * -1);
                }   
                else
                {
                    nX = Convert.ToInt32(Math.Ceiling((float)(nW - W) / 2) * -1);
                    nY = 0;    
                }
            }
            else if (wO < hO) // anh doc
            {
                nW = W;
                nH = Convert.ToInt32(Math.Ceiling(((float)hO * W) / wO));
                if (nH < H)
                {
                    nH = H;
                    nW = Convert.ToInt32(Math.Ceiling(((float)wO * nH) / hO));
                    nX = Convert.ToInt32(Math.Ceiling((float)(nW - W) / 2) * -1);
                    nY = 0;
                }else
                {
                    nX = 0;
                    nY = Convert.ToInt32(Math.Ceiling((float)(nH - H) / 2) * -1);
                }
            }
            else // anh vuong
            {
                nW = W;
                nH = H;
                nX = 0;
                nY = 0;
            }
        }
    }
}
